package com.wzl.thread.example;

import lombok.extern.slf4j.Slf4j;

/**
 * @author weizhongli
 * @since 2022-09-19 14:04
 * 20人秒杀10件商品
 */
@Slf4j
public class TcSeckillShop implements Runnable{

    //设计一个变量，用于表示商品的数量
    private static int id = 10;
    //表示客户名称变量
    private String userName;

    public TcSeckillShop(String userName) {
        this.userName = userName;
    }

    @Override
    public void run() {

        String name = Thread.currentThread().getName();
        System.out.println(userName + "正在使用" + name + "参与秒杀任务...");

        try {
            Thread.sleep(200);//为了逼真一点，休眠200毫秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized (TcSeckillShop.class){
            if(id > 0){
                log.info(userName + "使用" + name + "秒杀:" + id-- + "号商品成功啦！");
            }else {
                log.info(userName + "使用" + name + "秒杀失败了！");
            }
        }

    }
}
